<?php
namespace app\index\controller;

use app\admin\model\Active;
use app\admin\model\ChoujiangTimes;
use app\admin\model\ContestantNumber;
use app\admin\model\Participant;
use app\admin\model\VisitNumber;
use app\admin\model\VoteList;
use think\Cache;
use think\Controller;
use think\Request;
use think\Session;
use org\wechat\jssdk; 

class Wx extends Controller
{
    protected $session;

    protected $limit = 10;

    protected $pass_way = 'wx';

    protected $cn_arr = [1=>'一',2=>'二',3=>'三',4=>'四',5=>'五',6=>'六',7=>'七',8=>'八',9=>'九'];

    protected $active;

    protected $appID = 'wxd1eacc33429ff29f';

    protected $appSecret = '69cc2e0a4869a00f1093c141e96a94cf';

    protected $jssdk;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

	    $ip = $this->get_my_ip();
        if(strpos($ip,'101.226') === 0){
            echo '您的ip被屏蔽';die;
        }
        $this->session = Session::get('wx_item','vote');

        $id=$this->request->param('id');
        $this->active = Cache::get('active_'.$id);
        if(empty($this->active)){
            $active_model = new Active();
            $this->active = $active_model->where('id',$id)->find();
            Cache::set('active_'.$id,$this->active);
        }
        $is_test = Session::get('is_test','vote');
        if(empty($is_test)){
            $is_test = $this->request->param('is_test');
            if(!empty($is_test)){
                Session::set('is_test',1,'vote');
            }
        }

        $participant = new Participant();
        $p_count = $participant->where('active_id',$id)->count();
        $p_sum   = $participant->where('active_id',$id)->sum('support');
        $this->assign('p_count',$p_count);
        $this->assign('p_sum',$p_sum);
        
        $this->get_wx_config();
        $this->assign('is_test',$is_test);
    }

    protected function render_times($id){
        //获取缓存次数
        $browse_times = Cache::get('browse_time_'.$id);
        if(empty($browse_times)){
            $times_model = new VisitNumber();
            $model = $times_model->where('active_id',$id)->find();
            if(!$model){
                $model = VisitNumber::create([
                    'active_id'=>$id,
                    'times'=>1
                ]);
            }
            $browse_times = $model->times;
            Cache::set('browse_time_'.$id,$model->times);
        }else{
            //缓存自增
            Cache::inc('browse_time_'.$id);
        }

        //更新到数据库
        $update = Cache::get('update_browse_time_'.$id);
        if(empty($update)){
            Cache::set('update_browse_time_'.$id,1,300);
            $times_model = new VisitNumber();
            $model = $times_model->where('active_id',$id)->find();
            if($model->times < $browse_times){
                $model->times = $browse_times;
                $model->save();
            }
        }

        $this->assign('times',$browse_times);
    }

    public function index()
    {

       if(empty($this->session['user_id'])){
            $this->set_session();
       }

        // $param = $this->request->param();
        // if(empty($param['id'])){
        //     return view('public/error',['message'=>'活动id丢失']);
        // }
        // $active = $this->active;
        // if(empty($active)){
        //     $active_model = new Active();
        //     $active = $active_model->field('id,name,is_draw,weight,share_info,template,images,cover,end_img,status,draw_times,btn_info,theme_color,switch_id,switch_img,is_sign_up')->where('id',$param['id'])->find();
        // }

        // if(!$active){
        //     return view('public/error',['message'=>'活动不存在']);
        // }

        // if($active->status != 1){
        //     return view('public/error',['message'=>'活动未开放']);
        // }
        // $this->render_times($param['id']);

        // $where = ['active_id'=>['=',$param['id']],'status'=>['=',1]];
        // if(!empty($param['keywords'])){
        //     $where['contestant_number'] = ['exp','="'.$param['keywords'].'" or nickname like "%'.$param['keywords'].'%"'];
        // }
        // $to_channel = 0;
        // if(!empty($param['to_channel'])){
        //     $where['channel'] = $param['to_channel'];
        //     $to_channel = $param['to_channel'];
        // }
        // $order = 'create_time desc';
        // $order_type = 1;
        // if($param['id'] == 11){
        //     $order = 'contestant_number asc';
        // }
        // if(!empty($param['order']) && $param['order']==2){
        //     $order = 'support desc';
        //     $order_type = 2;
        // }
        // $start = 0;//开始从第一条查询
        // $boundary = time();//当前时间
        // $participant = new Participant();
        // $list  = $participant->where($where)->order($order)->limit($start,$this->limit)->select();

        // return view($active->template.'/wx/index',
        //     [
        //         'active'=>$active,
        //         'id'=>$param['id'],
        //         'channel'=>empty($param['channel_id'])?0:$param['channel_id'],
        //         'list'=>$list,
        //         'keywords'=>empty($param['keywords'])?'':$param['keywords'],
        //         'count'=>count($list),
        //         'boundary'=>$boundary.','.$this->limit,
        //         'order'=>$order_type,
        //         'se'=>$this->session,
        //         'number'=>$active->weight[$this->pass_way],
        //         'page'=>'index',
        //         'to_channel'=>$to_channel,

        //     ]
        // );
    }

    public function ajax_list(){
        $param = $this->request->param();
        if(!isset($param['boundary']) && empty($param['boundary'])){
            return json(['code'=>199,'message'=>'参数不完整']);
        }
        $active = $this->active;
        if(empty($active)){
            $active_model = new Active();
            $active = $active_model->field('id,weight,status')->where('id',$param['id'])->find();
        }

        if(!$active){
            return json(['code'=>199,'message'=>'活动不存在']);
        }

        if($active->status != 1){
            return json(['code'=>199,'message'=>'活动未开放']);
        }

        $array = explode(',',$param['boundary']);
        $boundary = $array[0];
        $start = $array[1];
        $where = ['active_id'=>['=',$param['id']],'status'=>['=',1],'update_time'=>['<',$boundary]];//过滤更新时间大于获取第一页的数据的时间
        if(!empty($param['keywords'])){
            $this->keywords = $param['keywords'];
            $where['contestant_number'] = ['exp','="'.$param['keywords'].'" or nickname like "%'.$param['keywords'].'%"'];
        }

        if(!empty($param['to_channel'])){
            $where['channel'] = $param['to_channel'];
        }
        $order = 'create_time desc';
        if($param['id'] == 11){
            $order = 'contestant_number asc';
        }
        if(!empty($param['order']) && $param['order']==2){
            $order = 'support desc';
        }
        $list = Participant::where($where)->order($order)->limit($start,$this->limit)->select();
        return json([
            'code'=>0,
            'message'=>'成功',
            'data'=>[
                'list'=>$list,
                'number'=>$active->weight[$this->pass_way],
                'count'=>count($list),
                'boundary'=>$boundary.','.($start+$this->limit),
            ]
        ]);
    }

    //报名列表
    public function signup(){
        $param = $this->request->param();
        if(empty($this->session['user_id'])){
            $this->set_session();
        }

        //判断
        if(empty($param['id'])){
            return view('public/error',['message'=>'活动id丢失']);
        }
        $active = $this->active;
        if(empty($active)){
            $active_model = new Active();
            $active = $active_model->field('id,name,is_draw,weight,share_info,template,images,business_images,cover,status,act_field,theme_color')->where('id',$param['id'])->find();

        }
        if(!$active){
            return view('public/error',['message'=>'活动不存在']);
        }

        if($active->status != 1){
            return view('public/error',['message'=>'活动未开放']);
        }
        //增加访问数
        $this->render_times($param['id']);

        //判断有无参加
        $participant = new Participant();
        $participant = $participant->where('user_id',$this->session['user_id'])->where('active_id',$param['id'])->find();
        $channel_id = empty($param['channel_id'])?'':$param['channel_id'];
        if(empty($channel_id) && $participant){
            $channel_id = $participant->channel;
        }

        return view($active->template.'/wx/signup',
            [
                'active'=>$active,
                'id'=>$param['id'],
                'keywords'=>'',
                'channel'=>$channel_id,
                'participant'=>$participant?$participant:null,
                'page'=>'sign_up'
            ]);
    }

    public function do_sign_up(){

        $param = $this->request->param();

        if(empty($this->session['user_id'])){
            return json(['code'=>196,'message'=>'登录超时']);
        }
        //判断
        if(empty($param['id'])){
            return json(['code'=>196,'message'=>'活动id丢失']);
        }
        if($param['id'] == 11){
            return json(['code'=>196,'message'=>'报名已结束']);
        }
        $active = $this->active;
        if(empty($active)){
            $active_model = new Active();
            $active = $active_model->field('id,weight,status,begin_at,end_at')->where('id',$param['id'])->find();
        }

        if(!$active->is_sign_up != 1){
            return json(['code'=>196,'message'=>'非法操作']);
        }

        if(!$active){
            return json(['code'=>196,'message'=>'活动不存在']);
        }

        if($active->status != 1){
            return json(['code'=>196,'message'=>'活动未开放']);
        }
        //验证活动时效性
        $res = check_active_time($active->begin_at,$active->end_at);
        if($res){
            return json(['code'=>$res['code'],'message'=>$res['message']]);
        }

        //验证信息
       $res = check_params($param);

        if($res){
            return json(['code'=>$res['code'],'message'=>$res['message']]);
        }

        //获取图片
        $images = [];
        if(!empty($param['imgs'])){

            foreach($param['imgs'] as $server_id){
                $images[] = $this->getImgUrl($server_id);
            }
        }

        // $voice = '';
        // $audio_time = 0;
        // if(!empty($param['voice'])){
        //     $voice = $this->getImgUrl($param['voice'],'','',11);
        //     $audio_time = $param['audio_time'];
        // }

        //有无参加
        
        $participant_model = new Participant();
        //$participant = $participant_model->where('user_id',$this->session['user_id'])->where('sign_pass_way',$this->pass_way)->where('active_id',$param['id'])->find();

        $participant_where = array(
            'user_id'       => $this->session['user_id'],
            'sign_pass_way' => $this->pass_way,
            'active_id'     => $param['id']

        );

       $participant = $participant_model->where($participant_where)->find();

        if($participant){
            //return json(['code'=>199,'message'=>'您已经报名参加过活动了']);
            $participant->nickname = empty($param['nickname'])?'':htmlspecialchars($param['nickname'],ENT_QUOTES);
            $participant->mobile = empty($param['mobile'])?'':htmlspecialchars($param['mobile'],ENT_QUOTES);
            $participant->birthday = empty($param['birthday'])?'':htmlspecialchars($param['birthday'],ENT_QUOTES);
            $participant->gender = empty($param['gender'])?'':htmlspecialchars($param['gender'],ENT_QUOTES);
            $participant->xuanyan = empty($param['xuanyan'])?'':htmlspecialchars($param['xuanyan'],ENT_QUOTES);
            $participant->body_height = empty($param['body_height'])?'':htmlspecialchars($param['body_height'],ENT_QUOTES);
            $participant->specialty = empty($param['specialty'])?'':htmlspecialchars($param['specialty'],ENT_QUOTES);
            $participant->school_name = empty($param['school_name'])?'':htmlspecialchars($param['school_name'],ENT_QUOTES);
            $participant->status = 0;
            if(!empty($images)){
                $participant->images = $images;
            }
            $participant->save();
        }else{
            //获取选手编号模型
            $number_model = new ContestantNumber();
            $model = $number_model->where('active_id',$param['id'])->find();

            //添加记录
            Participant::create([
                'active_id'=>$param['id'],
                'user_id'=>$this->session['user_id'],
                'sign_pass_way'=>$this->pass_way,
                'nickname'=>empty($param['nickname'])?'':htmlspecialchars($param['nickname'],ENT_QUOTES),
                'mobile'=>empty($param['mobile'])?'':htmlspecialchars($param['mobile'],ENT_QUOTES),
                'birthday'=>empty($param['birthday'])?'':htmlspecialchars($param['birthday'],ENT_QUOTES),
                'gender'=>empty($param['gender'])?'':htmlspecialchars($param['gender'],ENT_QUOTES),
                'body_height'=>empty($param['body_height'])?'':htmlspecialchars($param['body_height'],ENT_QUOTES),
                'specialty'=>empty($param['specialty'])?'':htmlspecialchars($param['specialty'],ENT_QUOTES),
                'school_name'=>empty($param['school_name'])?'':htmlspecialchars($param['school_name'],ENT_QUOTES),
                'xuanyan'=>empty($param['xuanyan'])?'':htmlspecialchars($param['xuanyan'],ENT_QUOTES),
                'status'=>0,
                'images'=>$images,
                'channel'=>empty($param['channel_id'])?0:$param['channel_id'],
                'contestant_number'=>$model?$model->number:0,
                //'audio_url'=>$voice,
                //'audio_time'=>$audio_time
            ]);

            if($model){
                //更新选手编号
                $model->setInc('number', rand(1,3));
            }

        }

        $back_json = array(
            'code'      => 0,
            'message'   => '报名成功'
        );

        return json($back_json);
    }

    public function do_vote(){

        // 禁止微信投票
        return json(['code'=>196,'message'=>'禁止微信投票']);

        $param = $this->request->param();
        if(empty($this->session['user_id'])){
            return json(['code'=>196,'message'=>'登录超时']);
        }
        //判断
        $last_time = Session::get('wx_last_vote_time','vote');
        if(empty($last_time)){
            $last_time = 0;
        }
	    $time = time();
        if($time-$last_time < 10){
            return json(['code'=>193,'message'=>'投票频繁请5秒后再尝试']);
        }
        if(empty($param['id'])){
            return json(['code'=>196,'message'=>'活动id丢失']);
        }
        $active = $this->active;
        if(empty($active)){
            $active_model = new Active();
            $active = $active_model->field('id,weight,status,begin_at,end_at,limit,limit_param')->where('id',$param['id'])->find();
        }

        if(!$active){
            return json(['code'=>196,'message'=>'活动不存在']);
        }

        if($active->status != 1){
            return json(['code'=>196,'message'=>'活动未开放']);
        }
        //验证活动时效性
        $end_at = $active->end_at;
        if($param['id'] == 8 || $param['id'] == 9){
            $end_at = 1508687999;
        }
        $res = check_active_time($active->begin_at,$end_at);
        if($res){
            return json(['code'=>$res['code'],'message'=>$res['message']]);
        }

        //获取投票信息
        //print_r($param);die;
        /*if($this->session['user_id'] == $param['participant_id']){
            return json(['code'=>170,'message'=>'不能自己给自己投票哦~']);
        }*/
        //循环所选限制方法,将每个方法所需参数分类
        $limit_param = $active->limit_param;
        $ip_info = ip_to_address($this->get_my_ip());
        $limit_param['ip_info'] = $ip_info;
        $limit_param['participant_id'] = $param['participant_id'];
        $limit_param['vote_user_id'] = $this->session['user_id'];
        $limit_param['device_id'] = empty($this->session['device'])?'':$this->session['device'];
        $limit_param['active_id'] = $param['id'];
        $limit_param['pass_way'] = $this->pass_way;
        //验证活动投票限制

        $res = check_function($active->limit,$limit_param);

        if($res){
            return json(['code'=>$res['code'],'message'=>$res['message']]);
        }
        //添加投票记录
        $res = VoteList::create([
            'active_id'=>$active->id,
            'participant_id'=>$param['participant_id'],
            'user_id'=>$this->session['user_id'],
            'nickname'=>$this->session['name'],
            'from_ip'=>$ip_info['ip'].$ip_info['message'],
            'from_device'=>empty($this->session['device'])?'':$this->session['device'],
            'from_pass_way'=>$this->pass_way,
            'create_time'=>time(),
            'channel'=>empty($param['channel_id'])?0:$param['channel_id']
        ]);

        if($res){
            //更新被投票人的数据
            $participant = new Participant();
            if($participant->where('active_id',$active->id)->where('user_id',$param['participant_id'])
                ->inc('pass_way_yqsbz')->inc('support',$active->weight[$this->pass_way])->update()){
                //设置上次投票时间
                Session::set('wx_last_vote_time',$time,'vote');
                return json(['code'=>0,'message'=>'投票成功,还可以继续哦！','support'=>$active->weight[$this->pass_way]]);
            }
        }
        return json(['code'=>199,'message'=>'投票失败','support'=>0]);
    }

    public function detail(){
        $param = $this->request->param();
        //获取访问人ID信息
        if(empty($this->session['user_id'])){
            $this->set_session();
        }
        $is_self = 0;
        $user_id = empty($param['user_id'])?'':$param['user_id'];
        if(empty($user_id)){
            $user_id = $this->session['user_id'];
            $is_self = 1;
            $this->assign('user_id',$user_id);
        }

        //判断
        if(empty($param['id'])){
            return view('public/error',['message'=>'活动id丢失']);
        }
        $active = $this->active;
        if(empty($active)){
            $active_model = new Active();
            $active = $active_model->field('id,name,is_draw,weight,share_info,template,images,business_images,cover,status,draw_times')->where('id',$param['id'])->find();

        }
        if(!$active){
            return view('public/error',['message'=>'活动不存在']);
        }

        if($active->status != 1){
            return view('public/error',['message'=>'活动未开放']);
        }
        //增加访问数
        $this->render_times($param['id']);

        //查询有无报名
        $participant = new Participant();
        $info = $participant->where('user_id',$user_id)->where('active_id',$param['id'])->find();

        //根据情况渲染视图
        if($info){
            switch ($info->status){
                case 0:
                    $no = '未审核';
                    break;
                case 1:
                    $no = $participant->where('active_id',$param['id'])->where('status',1)->where('support','>=',$info->support)
                        ->order('support desc,create_time asc')->count();
                    $no = '第'.$no.'名';
                    break;
                default:
                    $no = '审核未通过';
            }

            $is_draw = $active->is_draw;
            if($is_draw == 1){
                $times = new ChoujiangTimes();
                $times = $times->where('active_id',$param['id'])->where('user_id',$this->session['user_id'])->value('times');
                if($times >= $active->draw_times){
                    $is_draw = 0;
                }
            }

            return view($active->template.'/wx/detail',
                [
                    'active'=>$active,
                    'id'=>$param['id'],
                    'record'=>$info,
                    'is_self'=>$is_self,
                    'no'=>$no,
                    'se'=>$this->session,
                    'is_draw'=>$is_draw,
                    'keywords'=>'',
                    'page'=>'',
                    'number'=>$this->cn_arr[$active->weight[$this->pass_way]],
                    'channel'=>empty($param['channel_id'])?0:$param['channel_id'],

                ]);
        }else{
            if($is_self){
                $message = '您还没有报名';
                $url = url('index',['id'=>$param['id']]);
                $this->redirect('signup',['id'=>$param['id'],'channel_id'=>empty($param['channel_id'])?0:$param['channel_id']]);
            }else{
                $message = '报名人状态异常';
                $url = url('index/index',['id'=>$param['id']]);
            }
            return view('public/error',['message'=>$message,'url'=>$url]);
        }
    }

    public function intro(){
        $param = $this->request->param();
        //判断
        if(empty($param['id'])){
            return view('public/error',['message'=>'活动id丢失']);
        }
        //增加访问数
        $this->render_times($param['id']);
        $active = $this->active;
        if(empty($active)){
            $active_model = new Active();
            $active = $active_model->field('id,name,is_draw,share_info,template,images,cover,status,intro')->where('id',$param['id'])->find();
        }

        if(!$active){
            return view('public/error',['message'=>'活动不存在']);
        }

        if($active->status != 1){
            return view('public/error',['message'=>'活动未开放']);
        }
        return view($active->template.'/wx/intro',
            [
                'active'=>$active,
                'id'=>$param['id'],
                'keywords'=>'',
                'page'=>'intro',
                'channel'=>empty($param['channel_id'])?0:$param['channel_id']
            ]
        );
    }

    private function set_session(){
        //return true;
        $redirect_uri = $this->request->url(true);
        $params = $this->request->param();

        
        if(empty($params['code'])){

            //前往授权页面
            $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appID."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
            $this->redirect($url);
            exit();

        }else{
            //从授权页面跳转过来，带上了code参数
            $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->appID . "&secret=" . $this->appSecret . "&code=" . $params['code'] . "&grant_type=authorization_code";
            $rt = $this->getCurlData($url,2);
            $get_user  = json_decode($rt,true);

            if(empty($get_user['openid'])){
                $redirect_uri =  preg_replace("/\\?code=[^?&$]*/is", '', $redirect_uri);
                $redirect_uri =  preg_replace("/\\&code=[^?&$]*/is", '', $redirect_uri);
                $this->redirect($redirect_uri,302);
            }

            //$access_token = $this->jssdk->getAccessToken();
            $userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$get_user['access_token']."&openid=".$get_user['openid'];
            $rt = $this->getCurlData($userinfo_url,2);
            $data['item'] = json_decode($rt,true);

            //if(!$data['result']['success']){//code过期,过滤url中的code参数并跳转,会重新进入自身方法执行没有code的判断
           // if(!$get_user['access_token']){
                // $redirect_uri =  preg_replace("/\\?code=[^?&$]*/is", '', $redirect_uri);
                // $redirect_uri =  preg_replace("/\\&code=[^?&$]*/is", '', $redirect_uri);
                // $this->redirect($redirect_uri,302);
            //}else{//code 有效


                $data['item']['pass_way'] = $this->pass_way;
                $data['item']['name'] = htmlspecialchars(filterEmoji($data['item']['nickname']),ENT_QUOTES );
                $data['item']['user_id'] = $data['item']['openid'];
                Session::set('wx_item',$data['item'],'vote');
                $this->session = $data['item'];
                //$this->redirect($redirect_uri,302);
           // }
        }
    }

    //获取图片,原理是微信上传以后有个server_id,通过server_id访问公司接口,获得阿里云oss上的图片地址
    private function getImgUrl($media_id,$w='',$h='',$mode=0){

        $access_token = $this->jssdk->getAccessToken();
        $date_time    = date("Y-m-d H:i:s",time());
        $result       = $this->getmedia($access_token, $media_id, $date_time);
        return $result;

    }

    private function getmedia($access_token,$media_id,$foldername){
        $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$access_token."&media_id=".$media_id;

        if (!file_exists("./Uploads/".$foldername)) {
            mkdir("./Uploads/".$foldername, 0777, true);
        }
        $targetName ='/Uploads/'.$foldername.'/'.date('YmdHis').rand(1000,9999).'.jpg';

        $ch = curl_init($url); // 初始化
        $fp = fopen(".".$targetName, 'wb'); // 打开写入
        curl_setopt($ch, CURLOPT_FILE, $fp); // 设置输出文件的位置，值是一个资源类型
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_exec($ch);
        curl_close($ch);
        fclose($fp);
        return  "http://".$_SERVER['SERVER_NAME'].$targetName;
    }

    //curl访问自己公司的接口
    private function getCurlData($url,$dotype, $params=[]){
        $get = '';
        if($dotype==1){
            $get =file_get_contents($url);
        }elseif($dotype==2){
            $ch = curl_init() or die (curl_error());
            curl_setopt ($ch, CURLOPT_URL, $url);
            curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);//响应时间
            $get = curl_exec($ch);
            curl_close($ch);
        }
        return $get;
    }

    private function get_my_ip(){
        return $this->request->ip(0,true);
    }


    // public function wx_ajax(){

    //     header('Content-type: application/json');

    //     //获取回调函数名
    //     $jsoncallback = htmlspecialchars($_REQUEST['callback']);
    //     $URL          = htmlspecialchars($_REQUEST['url']);
    //    // $wxconfig     = wx_init($URL);

    //     $wxconfig = array();

    //     // vendor('wechat.class#jssdk');

    //     // $config = APP_DEBUG ? C("WECHAT_SDK_TEST"): C("WECHAT_SDK");
    //     // 这里配置了微信公众号的AppId和AppSecret

    //     $jssdk =new jssdk('wxd1eacc33429ff29f', 'a371dbd71e5085d0a0f5badbd628299d');

    //     $wxconfig = $jssdk->GetSignPackage();

    //     $json_data = json_encode($wxconfig);

    //     //输出jsonp格式的数据

    //     echo $jsoncallback . "(" . $json_data . ")";

    // }

    public function get_wx_config(){
        $this->jssdk = new Jssdk($this->appID, $this->appSecret);
        $wxconfig = $this->jssdk->GetSignPackage();
        $this->assign('wxconfig',$wxconfig);
    }

    public function get_access_token(){
        $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appID."&secret=".$this->appSecret;
        $resutl = $this->getCurlData($url,2);
        $resutl = json_decode($resutl,true);
        return $resutl['access_token'];
    }


}
